package com.fins.modules.utils;

import java.io.IOException;
import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * JSON 对象序列化(反)工具
 * 
 **/
public final class JsonUtils {

	private static ObjectMapper mapper = null;

	static {

		try {
			Class<?> mapClz = Class.forName("com.fins.modules.web.mvc.support.json.JacksonObjectMapper");
			mapper = (ObjectMapper) mapClz.newInstance();
		} catch (Exception e) {
		}

		if (mapper == null) {
			mapper = new ObjectMapper();
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 反序列化,不存在的属性异常
			mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);// 反序列化，兼容未标准的JSON写法(key)
			mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);// 反序列化，兼容未标准的JSON写法(value)
			mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); // 避免序列化异常
		}
	}

	private JsonUtils() {
	}

	private static ObjectMapper getMapper() {
		return mapper;
	}

	/**
	 * 将对象序列化为JSON字符串
	 * 
	 * @param obj
	 *            需要序列化的对象
	 * @return JSON字符串
	 */
	public static String toJson(Object obj) {
		StringWriter sw = new StringWriter();
		JsonGenerator gen = null;
		try {
			gen = new JsonFactory().createGenerator(sw);
			getMapper().writeValue(gen, obj);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (gen != null) {
				try {
					gen.close();
				} catch (IOException e) {
					gen = null;
				}
			}
		}
		return sw.toString();
	}

	/**
	 * 将不规范的JSON字符串转换成标准的 JSON格式
	 * 
	 * @param jsonString
	 *            字符串
	 * @return JSON字符串
	 */
	public static String formatToJson(String jsonString) {
		Map<String, Object> map = fromJson(jsonString);
		if (map != null) {
			return toJson(map);
		}
		return jsonString;
	}

	/**
	 * 将JSON字符串反序列化为对象实例
	 * 
	 * @param jsonString
	 *            JSON字符串
	 * @param clazz
	 *            要转换对象的类型
	 * @return 实例对象<T>
	 */
	public static <T> T fromJson(String jsonString, Class<T> clazz) {
		T t = null;
		try {
			if (jsonString != null) {
				t = getMapper().readValue(jsonString, clazz);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * 将JSON字符串反序列化为 Map
	 * 
	 * @param jsonString
	 *            JSON字符串
	 * @return Map对象,默认为LinkedHashMap
	 */
	public static Map<String, Object> fromJson(String jsonString) {
		Map<String, Object> map = null;
		try {
			if (jsonString != null) {
				map = getMapper().readValue(jsonString, new TypeReference<LinkedHashMap<String, Object>>() {
				});
			}
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}

	public static <T> T mapToBean(Map<?, ?> map, Class<T> clazz) {
		return getMapper().convertValue(map, clazz);
	}
}